package com.glorysoft.sunnypda.ui.eqpinfo

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.base.BaseViewModel
import com.glorysoft.sunnypda.databinding.ActivityEqpInfoBinding
import com.glorysoft.sunnypda.entity.EqpInfoEntity
import com.glorysoft.sunnypda.entity.SupEqpEntity
import com.glorysoft.sunnypda.entity.ui.ToastMessageBean
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import kotlinx.android.synthetic.main.activity_eqp_info.*

/**
 * FileName: EqpInfoActivity
 * Author: XJH
 * Date: 2022/3/21 17:46
 * Description:
 * History:
 */
class EqpInfoActivity : BaseActivity<EqpInfoViewModel,ActivityEqpInfoBinding>() {
    override fun layoutId(): Int {
        return R.layout.activity_eqp_info
    }

    override fun initVariableId(): Int {
        return BR.eqpInfoViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.add_eqp_access))
        viewModel.eqpInfoEntity = intent.getSerializableExtra("DATA") as EqpInfoEntity
        viewModel.supEqpItems.clear()
        if(viewModel.eqpInfoEntity.SUPEQPLIST != null
            && viewModel.eqpInfoEntity.SUPEQPLIST.isNotEmpty()
        ){
            for (i in 1..viewModel.eqpInfoEntity.SUPEQPLIST.size){
                viewModel.eqpInfoEntity.SUPEQPLIST.get(i-1).POSITION = i.toString()
            }
        }

        viewModel.supEqpItems.addAll(viewModel.eqpInfoEntity.SUPEQPLIST)
        val type = intent.getIntExtra("TYPE", 0)

        when(type){
            0 -> llReasonLayout.visibility = View.GONE
            1 -> tvReasonTitle.text = "迁入原因"
            2 -> tvReasonTitle.text = "迁出原因"
        }
    }

    override fun initData() {
        tvAddEqp.setOnClickListener{
            val customDialog = getCustomDialog(object : OnConfirmListener {
                override fun onConfirm(item : SupEqpEntity) {
                    viewModel.supEqpItems.forEach{
                        if (it.SUPEQPID == item.SUPEQPID){
                            viewModel.defUI.toastEvent.value = ToastMessageBean("当前设备已添加",false)
                            return
                        }
                    }
                    viewModel.supEqpItems.add(item)
                }

            })
            customDialog.show()
        }

        setRightListener(getString(R.string.confirm)){
            viewModel.eqpInfoEntity.SUPEQPLIST = viewModel.supEqpItems
            intent.putExtra("DATA",viewModel.eqpInfoEntity)
            setResult(RESULT_OK,intent)
            finish()
        }

    }

    private fun getCustomDialog(listener: OnConfirmListener): MaterialDialog {
        val dialog =  MaterialDialog(this)
            .cancelable(false)
            .cornerRadius(12f)
            .customView(R.layout.dialog_scan_start, noVerticalPadding = true)
            .lifecycleOwner(this)
        val contentView = dialog.getCustomView()
        val ivClose: ImageView = contentView.findViewById(R.id.ivClose)
        val etEqpWorkID: EditText = contentView.findViewById(R.id.etEqpWorkID)
        val etUser: EditText = contentView.findViewById(R.id.etUser)
        val tvConfirm: TextView = contentView.findViewById(R.id.tvConfirm)


        ivClose.setOnClickListener {
            dialog.dismiss()
        }

        tvConfirm.setOnClickListener {
            if (etEqpWorkID.text.toString().trim() == "" || etUser.text.toString().trim() == "") {
                Toast.makeText(this, "填写数据未完成", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            val entity = SupEqpEntity( etEqpWorkID.text.toString().trim(),
                etUser.text.toString().trim(),(viewModel.supEqpItems.size + 1).toString())
            listener.onConfirm(entity)
            dialog.dismiss()
        }

        return dialog
    }

    interface OnConfirmListener {
        fun onConfirm(entity : SupEqpEntity)
    }
}